package org.jfinanceobserver.utillities;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.jfinanceobserver.persistence.jpa.controller.QuotePropertyJpaController;
import org.jfinanceobserver.persistence.jpa.entities.QuoteProperty;
import org.jfinanceobserver.persistence.ConnFactory;

/**
 * This is the quotes properties class
 *
 * $Author: r78v10a07@gmail.com $ $LastChangedDate: 2013-02-11 13:03:43 +0000 (Mon, 11 Feb 2013) $ $LastChangedRevision: 2 $
 *
 * @since Feb 10, 2013
 */
public class QuoteProperties {

    private List<QuoteProperty> quotes = null;
    private StringBuilder values = null;
    private static QuoteProperties singleton;

    private QuoteProperties() {
    }

    /**
     * Return a QuoteProperties
     *
     * @return a QuoteProperties
     */
    public static synchronized QuoteProperties getInstance() {
        if (singleton == null) {
            singleton = new QuoteProperties();
            singleton.retrieveProperties();
        }
        return singleton;
    }

    private void retrieveProperties() {
        HashMap parm = new HashMap<>();
        parm.put("active", true);
        values = new StringBuilder();
        QuotePropertyJpaController qController = new QuotePropertyJpaController(ConnFactory.getInstance().getFactory());
        quotes = qController.useNamedQuery("QuoteProperty.findByActive", parm);

        if (!quotes.isEmpty()) {
            Iterator it = quotes.iterator();
            while (it.hasNext()) {
                QuoteProperty q = (QuoteProperty) it.next();
                values.append(q.getTag());
            }
        }
    }

    /**
     * Get the QuoteProperty list
     *
     * @return the QuoteProperty list
     */
    public List<QuoteProperty> getQuotes() {
        return quotes;
    }

    /**
     * Get the Yahoo URL values
     *
     * @return a string with the Yahoo URL values
     */
    public String getValues() {
        return values.toString();
    }
}
